﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

using Aspose.Cells;

using XMK.Framework.Model;
using XMK.Utils;
using XMK.Tadong.Biz;
using XMK.Tadong.Web.Language;

namespace XMK.Tadong.Web.Views.DataSearch
{
    public partial class XMKLogView : FileExplorerPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                startDate.Value = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                endDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
            }
        }

        protected void btnServerExport_Click(object sender, EventArgs e)
        {
            string logType = Request.Params.Get("logType");
            string startDate = Request.Params.Get("startDate");
            string endDate = Request.Params.Get("endDate");
            string userId = Request.Params.Get("uid");

            // 导出
            ResponseInfo exportRes = XMKLogBiz.QueryLog(
                ParseHelper.ParseEnum<Model.LogOperateType>(logType, Model.LogOperateType.Search),
                userId, DateTime.Parse(startDate), DateTime.Parse(endDate).AddDays(1),
                txtKeyw.Value.Replace("'","").Replace("%", "")
                );
            try
            {
                DataTable table = exportRes.Data as DataTable;
                ExportExcelWithAspose(table, Response);
            }
            catch (System.Exception ex)
            {
                LogHelper.WriteLog(LogType.Error, lang.Get("Log derived anomaly")+"," + ex.ToString());
            }
            
        }

        

        private static void ExportExcelWithAspose(DataTable dt, HttpResponse res)
        {
            if (dt != null)
            {
                try
                {
                    Workbook workbook = new Workbook();
                    Worksheet cellSheet = workbook.Worksheets[0];

                    cellSheet.Name = dt.TableName;

                    int rowIndex = 0;
                    int colIndex = 0;
                    int colCount = dt.Columns.Count;
                    int rowCount = dt.Rows.Count;

                    //列名的处理
                    for (int i = 0; i < colCount; i++)
                    {
                        cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Columns[i].ColumnName);
                        colIndex++;
                    }

                    Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];
                    style.Font.Name = "Arial";
                    style.Font.Size = 10;
                    Aspose.Cells.StyleFlag styleFlag = new Aspose.Cells.StyleFlag();
                    cellSheet.Cells.ApplyStyle(style, styleFlag);

                    rowIndex++;

                    for (int i = 0; i < rowCount; i++)
                    {
                        colIndex = 0;
                        for (int j = 0; j < colCount; j++)
                        {
                            cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Rows[i][j].ToString());
                            colIndex++;
                        }
                        rowIndex++;
                    }
                    cellSheet.AutoFitColumns();

                    res.Clear();
                    string fileName = string.Concat("log_", DateTime.Now.ToString("yyyyMMddHHmmssfff"), ".xlsx");
                    workbook.Save(fileName, SaveType.OpenInExcel, FileFormatType.Xlsx, res);
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLog(LogType.Error,ex.ToString());
                }
            }

        }
    }
}